home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15980 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  128 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: How do I put DIFFERENT classes in the same linked list?
  5. Message-ID: <marnoldDpJ9HK.41D@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4ka938$bj6@wintermute.ecs.fullerton.edu>
  8. Date: Mon, 8 Apr 1996 07:44:08 GMT
  9. Sender: marnold@netcom12.netcom.com
  10.  
  11. grosin@titan (Gil Rosin) writes:
  12.  
  13. >I'm trying to write a program where I need to maintain a linked list of
  14. >various items. Each item is a class, but each item is different from another
  15. >item. Let me give an example to clarify:
  16.  
  17. >A library catalog for example, there is a BOOK class and a MAGAZINE class, 
  18. >then there is an ITEM class from which BOOK and MAGAZINE are derivided (Tell
  19. >me if there is a better way to do this!!). ITEM has a ITEM *next pointer.
  20.  
  21. >Now, I want to be able to basically construct a linked list of BOOK and
  22. >MAGAZINE classes with out really knowing which one is which.
  23.  
  24. >Here is the source code I have worked up so far:
  25.  
  26. >#include <iostream.h>
  27. >class A
  28. > {
  29. >  public:
  30. >    A *next;
  31. >    A *prev;
  32. >    void Print();
  33. > };
  34. >class Demo
  35. > {
  36. >  public:
  37. >    void Insert(A *Temp);
  38. >    void Insert2(A *Temp);
  39. >    void Test(void);
  40. >    A *First;
  41. > };
  42. >class B : public A
  43. > {
  44. >  void Print();
  45. > };
  46. >class C : public A
  47. > {
  48. >  void Print();
  49. > };
  50. >main()
  51. > {
  52. >  Demo *T = new Demo;
  53. >  A *Test = new B;
  54. >  T->Insert(Test);
  55. >  Test = new C;
  56. >  T->Insert2(Test);
  57. >  T->Test();
  58. >  return 0;
  59. > }
  60. >void B::Print()
  61. > {
  62. >  cout << "I am in class B" << '\n';
  63. > }
  64. >void C::Print()
  65. > {
  66. >  cout << "I am in class C" << '\n';
  67. > }
  68. >void Demo::Insert(A *Temp)
  69. > {
  70. >  First = Temp;
  71. > }
  72. >void Demo::Insert2(A *Temp)
  73. > {
  74. >  First->next = Temp;
  75. > }
  76. >void Demo::Test(void)
  77. > {
  78. >  First->Print();
  79. >  First->next->Print();
  80. > }
  81. >void A::Print()
  82. > {
  83. >  cout << "I am in class A" << '\n';
  84. > }
  85.  
  86. >Now, as you can see, I want to have a linked list that contains EITHER
  87. >B or C Items, but in the void Demo::Test() function, I want to be able to
  88. >just call them blindly with out knowing which is which. When I compile this
  89. >I get NO errors, but when I run it the two lines that print out are the
  90. >class A print function. It should print out the class B and then the class C.
  91.  
  92. >How can I do this? I am not worrying about constructors/destructors etc
  93. >right now, just worrying about how to get it to work.
  94.  
  95. >Thanks. Please reply via email to grosin@titan.fullerton.edu.
  96.  
  97.  
  98. A::Print() needs to be a virtual function.
  99.  
  100. This will allow you to call the derived-class versions of Print() from
  101. code in A, or any place else you only have access to an A*, instead of
  102. a B* or C*.
  103.  
  104. For basic C++ programming information, "The C++ Programming Language" by
  105. Bjarne Stroustrup, is a good start.  Seems like you need to study the
  106. relationships between base and derived classes, virtual functions and
  107. when are where to use them.
  108.  
  109. Regards,
  110. -------------------------------------------------------------------------
  111. Matt Arnold                       |        | ||| | |||| |  | | || ||
  112. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  113. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  114. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  115. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  116. -------------------------------------------------------------------------
  117.